package com.google.code.chaotic.client.ui;

import java.util.List;

import com.allen_sauer.gwt.log.client.Log;
import com.google.code.chaotic.client.Util;
import com.google.code.chaotic.dto.LatLonDto;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Float;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.overlay.Icon;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.MarkerOptions;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;

public final class BulkMarkersViewer extends DialogBox {

	private MapWidget mapWidget;
	private Label descriptionLabel = new Label();

	//

	public BulkMarkersViewer(MapWidget mapWidget) {

		super(false, true);

		this.mapWidget = mapWidget;

		this.setText("Bulk Geo-markers Overview");
		this.setGlassEnabled(true);
		this.setAnimationEnabled(true);

		mapWidget.clearOverlays();
		FlowPanel vPanel = new FlowPanel();
		vPanel.add(descriptionLabel);
		vPanel.add(mapWidget);
		FlowPanel buttonsPanel = new FlowPanel();

		final Button okButton = new Button(" OK ");
		ClickHandler handler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide();
			}
		};
		okButton.addClickHandler(handler);
		buttonsPanel.add(okButton);
		vPanel.add(buttonsPanel);
		this.setWidget(vPanel);

		descriptionLabel.getElement().getStyle().setMargin(0.5, Unit.EM);
		
		Style buttonStyle = okButton.getElement().getStyle();
		buttonStyle.setMargin(0.5, Unit.EM);
		buttonStyle.setFloat(Float.RIGHT);

		// mapWidget.setSize("100%", "100%");
		mapWidget.setSize("880px", "440px");
		buttonsPanel.setWidth("100%");
	}

	public void markAll(List<LatLonDto> dtos) {
		MarkerOptions markerOption = MarkerOptions.newInstance();
		markerOption.setClickable(false);
		Icon icon = Icon.newInstance(Icon.DEFAULT_ICON);
		Util.scaleIcon(icon, 0.25);
		markerOption.setIcon(icon);

		for (LatLonDto dto : dtos) {
			Marker marker = new Marker(Util.toLatLng(dto), markerOption);
			mapWidget.addOverlay(marker);
		}
		Log.info("Markers added successfully!");
	}

	public void setDescription(String description) {
		descriptionLabel.setText(description);
	}

	public String getDescription() {
		return descriptionLabel.getText();
	}
}